package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.PicAdapter;
import com.dingwei.superiordiary.mvp.presenter.ExchangeGoodsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.weight.MyGridView;
import com.lihao.baselibrary.weight.MyRadioGroup;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/1/17.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 退货
 */

public class ExchangeGoodsActivity extends BaseActivity implements Contract.ReturnGoodsView {
    ExchangeGoodsPresenter exchangeGoodsPresenter = new ExchangeGoodsPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rb_one)
    RadioButton rbOne;
    @Bind(R.id.rb_two)
    RadioButton rbTwo;
    @Bind(R.id.rb_three)
    RadioButton rbThree;
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.gridview)
    MyGridView gridview;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.btn_submit)
    SuperButton btnSubmit;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.m_radio_group)
    MyRadioGroup mRadioGroup;

    private List<LocalMedia> photoList = new ArrayList<>();
    private String id;
    private String reason_tag = "1";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_exchenge_goods;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        id = getIntent().getStringExtra("id");


        tvTitle.setText(getIntent().getStringExtra("title"));
        // 控制输入框区域滑动不受父类滑块影响
        etContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        etContent.setOnTouchListener(new EditText_touch());


        mRadioGroup.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_one:
                        reason_tag = "1";
                        break;
                    case R.id.rb_two:
                        reason_tag = "2";
                        break;
                    case R.id.rb_three:
                        reason_tag = "3";
                        break;
                }
            }
        });
        rbOne.setChecked(true);

        photoList.clear();
        photoList.add(new LocalMedia());
        refreshPic();
    }


    private PicAdapter picAdapter;

    //刷新图片集合展示
    public void refreshPic() {
        if (picAdapter == null) {
            picAdapter = new PicAdapter(this, photoList,
                    DisplayUtil.getDisplayWidthPixels(this), new MyBack());
            gridview.setAdapter(picAdapter);
        } else {
            picAdapter.setList(photoList);
            picAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_submit:
                exchangeGoodsPresenter.service(id, etContent.getText().toString(), photoList, getIntent().getStringExtra("type"),reason_tag);
                break;
        }
    }

    @Override
    public void onFail() {
    }

    @Override
    public void onSuccess() {
        intent = new Intent(BaseActivity.context, WaitAuditActivity.class);
        intent.putExtra("id", id);
        startActivity(intent);
        finish();
    }


    /**
     * 让EditText 里面的内容可以上下滑动 （外层有Scrollview 的情况下）
     *
     * @author Administrator
     */
    class EditText_touch implements View.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            // 解决scrollView中嵌套EditText导致不能上下滑动的问题
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return false;
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            if (data != null) {

                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                photoList.clear();
                for (LocalMedia localMedia : selectList) {
                    if (!TextUtils.isEmpty(localMedia.getCompressPath())) {
                        photoList.add(localMedia);
                    }

                }
                photoList.add(new LocalMedia());
                if (photoList.size() == 7) {
                    photoList.remove(photoList.size() - 1);
                }
                refreshPic();
            }
        }
    }

    /**
     * 点击图片上删除按钮的回调函数
     */
    class MyBack implements PicAdapter.Back {

        @Override
        public void back() {

            List<LocalMedia> localMediaList = new ArrayList<>();
            localMediaList.addAll(photoList);
            for (int i = 0; i < photoList.size(); i++) {
                if (TextUtils.isEmpty(photoList.get(i).getCompressPath())) {
                    localMediaList.remove(i);
                    break;
                }
            }
            PictureSelector.create(ExchangeGoodsActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .theme(R.style.picture_white_style)
                    .imageSpanCount(3)
                    .maxSelectNum(6)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .previewImage(true)
                    .isCamera(true)
                    .isZoomAnim(true)
                    .selectionMedia(localMediaList)
                    .enableCrop(false)// 是否裁剪 true or false
                    .compress(true)// 是否压缩 true or false
                    .previewEggs(true) // 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                    .minimumCompressSize(100) // 小于100kb的图片不压缩
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }

        @Override
        public void delete(int index) {
            // TODO Auto-generated method stub
            if (photoList.size() == 6) {
                photoList.remove(index);
                boolean isAdd = true;
                for (int i = 0; i < photoList.size(); i++) {
                    LocalMedia photoItem = photoList.get(i);
                    if (TextUtils.isEmpty(photoItem.getCompressPath())) {
                        isAdd = false;
                        break;
                    }
                }
                if (isAdd) {
                    photoList.add(new LocalMedia());
                }
            } else {
                photoList.remove(index);
            }
            refreshPic();
        }

    }
}
